package capacity

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/capacity"
	capacityReq "github.com/flipped-aurora/gin-vue-admin/server/model/capacity/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type CapacityApi struct {
}

var capacitysService = service.ServiceGroupApp.CapacityServiceGroup.CapacityService

// CreateCapacity 创建capacity表
// @Tags Capacity
// @Summary 创建capacity表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body capacity.Capacity true "创建capacity表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /capacitys/createCapacity [post]
func (capacitysApi *CapacityApi) CreateCapacity(c *gin.Context) {
	var capacitys capacity.Capacity
	err := c.ShouldBindJSON(&capacitys)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := capacitysService.CreateCapacity(&capacitys); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteCapacity 删除capacity表
// @Tags Capacity
// @Summary 删除capacity表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body capacity.Capacity true "删除capacity表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /capacitys/deleteCapacity [delete]
func (capacitysApi *CapacityApi) DeleteCapacity(c *gin.Context) {
	var capacitys capacity.Capacity
	err := c.ShouldBindJSON(&capacitys)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := capacitysService.DeleteCapacity(capacitys); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteCapacityByIds 批量删除capacity表
// @Tags Capacity
// @Summary 批量删除capacity表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除capacity表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /capacitys/deleteCapacityByIds [delete]
func (capacitysApi *CapacityApi) DeleteCapacityByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := capacitysService.DeleteCapacityByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateCapacity 更新capacity表
// @Tags Capacity
// @Summary 更新capacity表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body capacity.Capacity true "更新capacity表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /capacitys/updateCapacity [put]
func (capacitysApi *CapacityApi) UpdateCapacity(c *gin.Context) {
	var capacitys capacity.Capacity
	err := c.ShouldBindJSON(&capacitys)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := capacitysService.UpdateCapacity(capacitys); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindCapacity 用id查询capacity表
// @Tags Capacity
// @Summary 用id查询capacity表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query capacity.Capacity true "用id查询capacity表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /capacitys/findCapacity [get]
func (capacitysApi *CapacityApi) FindCapacity(c *gin.Context) {
	var capacitys capacity.Capacity
	err := c.ShouldBindQuery(&capacitys)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if recapacitys, err := capacitysService.GetCapacity(capacitys.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"recapacitys": recapacitys}, c)
	}
}

// GetCapacityList 分页获取capacity表列表
// @Tags Capacity
// @Summary 分页获取capacity表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query capacityReq.CapacitySearch true "分页获取capacity表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /capacitys/getCapacityList [get]
func (capacitysApi *CapacityApi) GetCapacityList(c *gin.Context) {
	var pageInfo capacityReq.CapacitySearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := capacitysService.GetCapacityInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
